package com.hyyt.shopjsp.basic.controller;

import com.hyyt.shopjsp.basic.pojo.*;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.basic.service.IBasicActorService;
import com.hyyt.shopjsp.basic.service.IBasicFunctionsService;
import com.hyyt.shopjsp.basic.service.IBasicPurviewService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 7:06.
 */
@Controller
@RequestMapping("/back/actorPurview")
public class BasicActorPurviewController extends BaseController {

    private Logger log = Logger.getLogger(BasicActorPurviewController.class);
    private String basePath = "/back/basic/actorPurview/";

    @Resource
    private IBasicActorPurviewService basicActorPurviewService;
    @Resource
    private IBasicPurviewService basicPurviewService;
    @Resource
    private IBasicActorService basicActorService;

    /**
     * 进入权限页面
     *
     * @param actorId
     * @param param
     * @return
     */
    @RequestMapping("assignPurview")
    public String assignPurview(Integer actorId, Map<String, Object> param) {
        param.put("actorId", actorId);
        BasicActor ba=basicActorService.loadBasicActor(actorId);
        param.put("actorName", ba.getActorName());
        return basePath + "actorPurviewList";
    }

    /**
     * 获得所有权限
     *
     * @param actorId
     * @param response
     */
    @RequestMapping(value = "getPurviewListByActorId", method = RequestMethod.POST)
    @ResponseBody
    public void getPurviewListByActorId(Integer actorId, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();// 定义map
        params.put("sortColumns", "sortCode asc");
        List<BasicPurview> purviewList = basicPurviewService.queryBasicPurview(params);
        params.clear();
        params.put("actorId", actorId);
        List<BasicActorPurview> actorPurviewList = basicActorPurviewService.queryBasicActorPurview(params);
        params.put("purviewList", purviewList);
        params.put("actorPurviewList", actorPurviewList);
        params.put("actorId", actorId);
        ResultMsg.create(params, response);
    }

    /**
     * 获得所有权限
     *
     * @param actorId
     * @param purviewId
     */
    @RequestMapping(value = "getBasicActorPurviewByActorAndPurview", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getBasicActorPurviewByActorAndPurview(Integer actorId, Integer purviewId) {
        if (Utils.objectIsNotEmpty(actorId) && Utils.objectIsNotEmpty(purviewId)) {
            BasicActorPurview bap = basicActorPurviewService.loadBasicActorPurview(actorId,purviewId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(bap));
        }
        return ResultMsg.create(false);
    }


    /**
     * 保存角色对应权限
     * @return
     */
    @RequestMapping(value = "saveActorPurview", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveActorPurview(Integer actorId, Integer purviewId, String functions){
        boolean result=true;
        if(Utils.objectIsNotEmpty(actorId)&&Utils.objectIsNotEmpty(purviewId)){
            result=basicActorPurviewService.saveActorPurview(purviewId, actorId, functions);
        }
        return ResultMsg.create(result);
    }

    /**
     * 保存角色对应权限
     * @return
     */
    @RequestMapping(value = "deleteActorPurview", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteActorPurview(Integer actorId, Integer purviewId){
        if(Utils.objectIsNotEmpty(actorId)&&Utils.objectIsNotEmpty(purviewId)){
            basicActorPurviewService.deleteBasicActorPurview(actorId, purviewId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }

    }
}
